package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.pay.feign.WxPayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @description:
 * @author: 大佬味的小男孩
 * @date: 2020-08-04 21:00
 **/
@Controller
@RequestMapping("wxpay")
public class WxPayController {
    @Autowired
    private WxPayFeign wxPayFeign;
    @Autowired
    private OrderFeign orderFeign;

    @GetMapping
    public String wxPay(String orderId, Model model) {
        //1.根据orderId查询订单是存在，如果不存在 跳转到错误页面
        Order order = (Order) orderFeign.findById(orderId).getData();
        if (order == null) {
            return "fail";
        }
        //2.根据订单的支付状态进行判断，如果已支付跳转到错误页面
        if (!"0".equals(order.getPayStatus())) {
            return "fail";
        }
        //3.基于wxPayFeign调用统计下单接口，并生成微信支付二维码
        Result result = wxPayFeign.nativePay(orderId, order.getPayMoney());
        if (result.getData() == null) {
            return "fail";
        }
        //4.封装支付返回结果
        Map resultMap = (Map) result.getData();
        resultMap.put("orderId", orderId);
        resultMap.put("payMoney", order.getPayMoney());
        model.addAllAttributes(resultMap);
        return "wxpay";
    }

    //订单支付成功跳转页面
    @RequestMapping("paysuccess")
    public String paysuccess(Integer payMoney, Model model) {
        model.addAttribute("payMoney", payMoney);
        return "paysuccess";
    }
}
